package com.open.cinema.system.manager;

import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 任务管理器
 * 单例模式
 */
public class TimerTaskManager {

    //线程池
    private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);

    private static volatile TimerTaskManager singleton;

    private TimerTaskManager() {}

    //双重检查
    public static TimerTaskManager getInstance() {
        if (singleton == null) {
            synchronized (TimerTaskManager.class) {
                if (singleton == null) {
                    singleton = new TimerTaskManager();
                }
            }
        }
        return singleton;
    }

    //执行任务
    public void execute(TimerTask task)
    {
        executor.schedule(task, 20, TimeUnit.MILLISECONDS);
    }
}
